#!/bin/sh
#
# Wrapper script for cvmfs_pull. Create /etc/cvmfs/replica.conf and/or
# /etc/cvmfs/replica.local to set the parameters.  This script is triggered
# by the CernVM-FS replica cgi script.
#
# Command line options overwrite the parameter file settings.
#

usage() {
   echo "$0 [-b <base directory>] [-l <log file>] [-r <repository list>][-a <additional options>] [-u <url>] [-n <number of parallel downloads>]"
}


[ -f /etc/cvmfs/replica.conf ] && . /etc/cvmfs/replica.conf
[ -f /etc/cvmfs/replica.local ] && . /etc/cvmfs/replica.local
ADDITIONAL_OPTIONS=

if [ -z $CVMFS_REPLICA_PARALLEL ]; then
   CVMFS_REPLICA_PARALLEL=128
fi

if [ -z $CVMFS_REPLICA_URL ]; then
   CVMFS_REPLICA_URL=http://cernvm-bkp.cern.ch/opt
fi

if [ -z $CVMFS_REPLICA_BASE ]; then
   CVMFS_REPLICA_BASE=/var/cvmfs
fi

if [ -z $CVMFS_REPLICA_LOG ]; then
   CVMFS_REPLICA_LOG=/var/log/cvmfs_replica
fi
if [ ! -z "$CVMFS_REPLICA_ADDITIONAL" ]; then
  ADDITIONAL_OPTIONS=$CVMFS_REPLICA_ADDITIONAL 
fi

if [ -z "$CVMFS_REPLICA_REPOSITORY_LIST" ]; then
   CVMFS_REPLICA_REPOSITORY_LIST="alice atlas atlas-condb cms lhcb grid sft hone hepsoft na61 lcd boss geant4"
fi

while getopts "b:l:r:a:u:n:" OPTION
do
  case $OPTION in
    b)
      CVMFS_REPLICA_BASE=$OPTARG
    ;;
    l)
      CVMFS_REPLICA_LOG=$OPTARG
    ;;
    a)
      ADDITIONAL_OPTIONS=$OPTARG
    ;;
    u)
      CVMFS_REPLICA_URL=$OPTARG
    ;;
    n)
      CVMFS_REPLICA_PARALLEL=$OPTARG
    ;;  
    r)
      CVMFS_REPLICA_REPOSITORY_LIST=$OPTARG
    ;;
    ?)
      echo "Invalid option"
      exit 1
    ;;
  esac
done

if [ -z $CVMFS_REPLICA_BASE ] || [ -z $CVMFS_REPLICA_LOG ]; then
  usage
  exit 1
fi

#for repository in alice atlas atlas-condb cms lhcb grid sft hone hepsoft na61 lcd boss geant14
for repository in $CVMFS_REPLICA_REPOSITORY_LIST
do
  /bin/date | tee -a $CVMFS_REPLICA_LOG
  
  initial_options=
  if [ -d $CVMFS_REPLICA_BASE/$repository/pub/catalogs ]; then
    snapshot=snapshot-`date "+%Y%m%d%H%M%S"`
    snapshot_progress="$snapshot.in_progress"
    cp -a $CVMFS_REPLICA_BASE/$repository/pub/catalogs/ \
          $CVMFS_REPLICA_BASE/$repository/pub/$snapshot_progress
  else
    snapshot="snapshot-initial"
    snapshot_progress="$snapshot.in_progress"
    mkdir -p $CVMFS_REPLICA_BASE/$repository/pub/$snapshot_progress
    initial_options="-i"
  fi

  /usr/bin/cvmfs_pull -d $CVMFS_REPLICA_BASE/$repository/pub \
    -u ${CVMFS_REPLICA_URL}/${repository} \
    -t 10 -r 3 -n $CVMFS_REPLICA_PARALLEL  \
    -k /etc/cvmfs/keys/cern.ch.pub \
    -m $repository.cern.ch \
    -s $snapshot_progress -e $initial_options $ADDITIONAL_OPTIONS 2>&1 | tee -a $CVMFS_REPLICA_LOG
      
  if [ $PIPESTATUS -eq 0 ]; then
    mv $CVMFS_REPLICA_BASE/$repository/pub/$snapshot_progress $CVMFS_REPLICA_BASE/$repository/pub/$snapshot
    touch $CVMFS_REPLICA_BASE/$repository/pub/$snapshot/.cvmfs_master_replica
    ln -fTs $snapshot $CVMFS_REPLICA_BASE/$repository/pub/catalogs 2>&1 | tee -a $CVMFS_REPLICA_LOG
  else
    snapshot_failed="$snapshot.failed"
    rm -rf $CVMFS_REPLICA_BASE/$repository/pub/$snapshot_failed
    mv $CVMFS_REPLICA_BASE/$repository/pub/$snapshot_progress \
       $CVMFS_REPLICA_BASE/$repository/pub/$snapshot_failed
  fi
  
  /bin/date | tee -a $CVMFS_REPLICA_LOG
done
